//
//  ViewController.m
//  卡片堆叠效果实现
//
//  Created by qianfeng on 16/1/9.
//  Copyright (c) 2016年 qianfeng. All rights reserved.
//

#import "ViewController.h"

#define SCREEN_HEIGTH [UIScreen mainScreen].bounds.size.height

#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width

@interface ViewController ()<UIScrollViewDelegate>
//距离窗口顶部的距离
@property (nonatomic,assign) CGFloat top;
//视图与视图之间的距离
@property (nonatomic,assign) CGFloat distance;
//scrollView的作用为提供参考数值 并不添加任何视图在里面（无复用）
@property (nonatomic,strong) UIScrollView * scrollView;
//用来存储数据
@property (nonatomic,strong) NSMutableArray * dataArr;
//用来存储view
@property (nonatomic,strong) NSMutableArray * viewArr;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor darkGrayColor];
    //添加scrollView 数值参考使用
    [self.view addSubview:self.scrollView];
    //调用一次协议方法 初始化多个view的位置
    [self scrollViewDidScroll:self.scrollView];
}
//初始化数组
-(NSMutableArray *)dataArr{
    
    if (!_dataArr) {
        _dataArr = [[NSMutableArray alloc] initWithObjects:@"",@"",@"",@"",@"",@"",@"",@"",nil];
        
    }
    return _dataArr;
}

-(CGFloat)top{
    
    if (!_top) {
        _top = 44.0;
    }
    return _top;
}

-(CGFloat)distance{
    
    if (!_distance) {
        _distance = 160;
    }
    return _distance;
}

-(UIScrollView *)scrollView{
    
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
        _scrollView.delegate = self;
        _scrollView.contentSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGTH+self.distance*(self.dataArr.count-1.5));
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.showsVerticalScrollIndicator = NO;
        
        //添加多个view在self.view上面
        for (UIView * view in self.viewArr) {
            [self.view addSubview:view];
        }
    }
    return _scrollView;
}
//初始化多个view
-(NSMutableArray *)viewArr{
    
    if (!_viewArr) {
        _viewArr = [[NSMutableArray alloc] init];
        
        [self.dataArr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            
            UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0 , SCREEN_WIDTH, SCREEN_HEIGTH)];
            view.backgroundColor = [UIColor whiteColor];
            view.layer.transform = CATransform3DMakeScale(0.6, 0.6, 1);
            
            /**************添加子视图到view上****************/
            
            UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 1, SCREEN_WIDTH, 63)];
            label.backgroundColor = [UIColor lightGrayColor];
            label.text = [NSString stringWithFormat:@"第%d页",idx];
            label.textColor = [UIColor whiteColor];
            label.textAlignment = 1;
            [view addSubview:label];
            
            [_viewArr addObject:view];
            
            /**********************************************/
        }];
    }
    return _viewArr;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    [self.viewArr enumerateObjectsUsingBlock:^(UIView * view, NSUInteger idx, BOOL *stop) {
        
        NSInteger value = self.distance;
        
        /*************设置位置***************/
        CGRect rect = view.frame;
        //view最底位置
        NSInteger min_y = self.top + value*idx;
        rect.origin.y = min_y - (scrollView.contentOffset.y);
        
        //view的最高位置
        if (scrollView.contentOffset.y >= value*idx-value*pow(2.0, idx)/pow(2.0, self.dataArr.count)){
            rect.origin.y = self.top+value*pow(2.0, idx)/pow(2.0, self.dataArr.count);
        }
        if (scrollView.contentOffset.y > scrollView.contentSize.height- SCREEN_HEIGTH) {
            rect.origin.y = rect.origin.y -(scrollView.contentOffset.y -(scrollView.contentSize.height-SCREEN_HEIGTH));
        }
        view.frame = rect;
        
        /*************设置大小***************/
        CGFloat scale = 0.60;
        scale = (rect.origin.y*0.75 +60)/1000+scale >=0.9?0.9:(rect.origin.y*0.75 +50)/1000+scale;
        view.layer.transform = CATransform3DMakeScale(scale, scale, 1);
    }];
}

@end
